<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/20 0020
 * Time: 16:44
 */

namespace app\api\controller\jiaoyi;


use app\api\controller\Common;
use app\api\service\TraderService;

class Trade extends Common
{
    public function _empty($name)
    {
        die('非法访问！你的IP已被监控！');
    }

    /**
     * @name 发布求购
     * @author zay
     * @Date 2020/3/20 0020 17:15
     */
    public function qiugou()
    {
        try {
            $appdata = $this->appdata;
            $userinfo = $this->userinfo;
            $num = $appdata['num'];
            $price = $appdata['price'];
            $secpwd = $appdata['secpwd'];
            if (empty($num) || empty($price) || empty($secpwd)) {
                throw new \Exception('缺少必要的参数');
            }
            //验证支付密码
            if (md5($secpwd) != $userinfo['secpwd']) {
                throw new \Exception('支付密码不正确');
            }
            /*//获取可以求购的时间
            $open_time = setting('open_time');
            if (date('H') < $open_time) {
                throw new \Exception('还未开盘');
            }*/
            if ($price!=setting('current_price')) {
                throw new \Exception('求购单价格必须与当前价格一样');
            }
            //限制每个人最多可以挂几单
            $guadan_num = Db('ppdd')->field('id')->where(['p_id' => $userinfo['id']])->whereIn('zt', [0,1])->count();
            $guadan_max = setting('guadan_max');
            if ($guadan_num + 1 > $guadan_max) {
                throw new \Exception('同一个账号最多可以挂' . $guadan_max . '单');
            }
            $data = [
                'p_order_sn' => create_sn(),
                'num' => $num,
                'p_id' => $userinfo['id'],
                'p_user' => $userinfo['uname'],
                'time' => time(),
                'price' => $price,
                'total_money' => $price * $num,
                'zt' => 0,
                'update_time' => time(),
            ];
            $res = DB('ppdd')->insert($data);
            if (!$res) {
                throw new \Exception('发不成功');
            }
            apifasong(['zt' => 1, 'msg' => '发布成功']);
        } catch (\Exception $e) {
            apifasong(['zt' => 0, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * @name 出售
     * @author zay
     * @Date 2020/3/20 0020 18:03
     */

    public function sell()
    {
        $appdata = $this->appdata;
        $userinfo = $this->userinfo;
        return TraderService::service()->sell($appdata, $userinfo);
    }

    /**
     * @name 求购列表
     * @author zay
     * @Date 2020/3/21 0021 13:09
     */

    public function buylist()
    {
        $appdata = $this->appdata;
        $userinfo = $this->userinfo;
        return TraderService::service()->buylist($appdata, $userinfo, 1);
    }

    /**
     * @name 取消订单
     * @author zay
     * @Date 2020/3/21 0021 13:09
     */

    public function cancel()
    {
        $appdata = $this->appdata;
        $userinfo = $this->userinfo;
        return TraderService::service()->cancel($appdata, $userinfo);
    }

    /**
     * @name 支付订单
     * @author zay
     * @Date 2020/3/21 0021 13:09
     */

    public function pay()
    {
        $appdata = $this->appdata;
        $userinfo = $this->userinfo;
        return TraderService::service()->pay($appdata, $userinfo);
    }

    /**
     * @name 放币（交易完成）
     * @author zay
     * @Date 2020/3/21 0021 13:09
     */

    public function confirm()
    {
        $appdata = $this->appdata;
        $userinfo = $this->userinfo;
        return TraderService::service()->confirm($appdata, $userinfo);
    }

    /**
     * @name 投诉
     * @author zay
     * @Date 2020/3/21 0021 16:15
     */

    public function tousu()
    {
        $appdata = $this->appdata;
        $userinfo = $this->userinfo;
        return TraderService::service()->tousu($appdata, $userinfo);
    }
    /**
     * @name 申诉
     * @author zay
     * @Date 2020/3/21 0021 16:15
     */

    public function appeal()
    {
        $appdata = $this->appdata;
        $userinfo = $this->userinfo;
        return TraderService::service()->appeal($appdata, $userinfo);
    }

    /**
     * @name 交易详情
     * @author zay
     * @Date 2020/3/21 0021 16:15
     */

    public function detail()
    {
        $appdata = $this->appdata;
        $userinfo = $this->userinfo;
        return TraderService::service()->detail($appdata, $userinfo);
    }

    /**
     * @name 历史委托
     * @author zay
     * @Date 2020/3/21 0021 16:15
     */

    public function trustlist()
    {
        $appdata = $this->appdata;
        $userinfo = $this->userinfo;
        return TraderService::service()->buylist($appdata, $userinfo, 2);
    }

    /**
     * @name 取消委托
     * @author zay
     * @Date 2020/3/23 0023 09:53
     */

    public function trustcancel()
    {
        $appdata = $this->appdata;
        $userinfo = $this->userinfo;
        return TraderService::service()->trustcancel($appdata, $userinfo);
    }

    /**
     * @name 交易记录
     * @author zay
     * @Date 2020/3/23 0023 09:53
     */

    public function jiaoyilist()
    {
        $appdata = $this->appdata;
        $userinfo = $this->userinfo;
        return TraderService::service()->jiaoyilist($appdata, $userinfo);
    }

    /**
     * @name 日K线
     * @author zay
     * @Date 2020/3/23 0023 16:00
     */

    public function kday()
    {
        $appdata = $this->appdata;
        $userinfo = $this->userinfo;
        return TraderService::service()->kday($appdata, $userinfo);
    }

    /**
     * @name 小时K线
     * @author zay
     * @Date 2020/3/23 0023 16:00
     */

    public function khours()
    {
        $appdata = $this->appdata;
        $userinfo = $this->userinfo;
        return TraderService::service()->khours($appdata, $userinfo);
    }

    /**
     * @name 获取涨幅
     * @author zay
     * @Date 2020/3/25 0025 13:27
     */

    public function increase()
    {
        $appdata = $this->appdata;
        $userinfo = $this->userinfo;
        return TraderService::service()->increase($appdata, $userinfo);

    }
    /**
     * @name 获取投诉
     * @author zay
     * @Date 2020/3/25 0025 13:27
     */

    public function tousu_info()
    {
        $appdata = $this->appdata;
        $userinfo = $this->userinfo;
        return TraderService::service()->tousu_info($appdata, $userinfo);

    }
}